package cn.com.bluemoon.daps.standard.mapper;

import cn.com.bluemoon.daps.standard.vo.BasicStandardAndDicVo;
import cn.com.bluemoon.daps.standard.entity.DapStandardBasic;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 基础标准表 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Repository
public interface DapStandardBasicMapper extends BaseMapper<DapStandardBasic> {

    /**
     * 根据标准唯一id更新发布信息
     *
     * @param standardUniqueId 同一标准的唯一id
     * @param bool             修改 字段publish_newest
     */
    @Update("update dap_standard_basic set update_time=current_timestamp(), publish_newest=#{bool} " +
            " where standard_unique_id =#{standardUniqueId} and publish_newest <> #{bool}  and bm_status=1 ")
    void updatePublishNewestByStandardUniqueId(@Param("standardUniqueId") String standardUniqueId, @Param("bool") Boolean bool);

    /**
     * 获取库表中去重的标准中英文名称
     *
     * @return
     */
    @Select("select distinct name, eng_name from dap_standard_basic where 1=1 and bm_status=1")
    List<Map<String, String>> findDistinctName();

    /**
     * 获取关联模型信息（已发布）
     *
     * @param standardUniqueId 标准唯一id
     * @return
     */
    @Select("select a.name as model_name,\n" +
            "       a.system_id,\n" +
            "       b.name as system_name,\n" +
            "       c.data_model_id as model_id,\n" +
            "       c.id as model_table_id,\n" +
            "       c.eng_name as db_table_name,\n" +
            "       d.id as model_field_id,\n" +
            "       d.field_name as model_field_name,\n" +
            "       d.length as model_field_length,\n" +
            "       d.data_type as model_field_type\n" +
            "from dap_data_model a\n" +
            "       inner join dap_system_info b on a.system_id = b.id and b.bm_status = 1\n" +
            "       left join dap_data_model_table c on c.data_model_id = a.id and b.bm_status = 1\n" +
            "       left join dap_data_model_table_field d on d.model_table_id = c.id and b.bm_status = 1\n" +
            "where d.standard_id =#{standardUniqueId} and d.data_newest = 0 and c.data_newest = 0 and a.bm_status=1")
    List<BasicStandardAndDicVo.ModelTableInfo> relateModelTableData(@Param("standardUniqueId") String standardUniqueId);

    /**
     * 根据标准唯一id，逻辑删除基础标准信息（一条或多条）
     *
     * @param standardUniqueId
     * @return
     */
    @Update("update dap_standard_basic set bm_status=-1  where standard_unique_id =#{standardUniqueId}")
    int deleteLogicByStandardUniqueId(@Param("standardUniqueId") String standardUniqueId);

    /**
     * 根据标准唯一id，物理删除基础标准信息（一条或多条）
     *
     * @param standardUniqueId
     * @return
     */
    @Delete("delete from dap_standard_basic where standard_unique_id =#{standardUniqueId}")
    int deleteByStandardUniqueId(String standardUniqueId);
}
